package com.vmj.natives.java.security;

import com.vmj.heap.Method;
import com.vmj.heap.VMObject;
import com.vmj.instructions.base.MethodInvokeLogic;
import com.vmj.natives.NativeMethod;
import com.vmj.natives.Registry;
import com.vmj.rtda.Frame;
import com.vmj.rtda.LocalVars;
import com.vmj.rtda.OperandStack;

/**
 * Created by yangshoujun on 2017/9/17 下午2:54.
 */
public class AccessController {

    public static final java.lang.String jlClass = "java/security/AccessController";

    static NativeMethod doPrivileged = new NativeMethod() {
        public void execute(Frame frame) {
            LocalVars vars = frame.getLocalVars();
            VMObject action = vars.getRef(0);

            OperandStack stack = frame.getOperandStack();
            stack.pushRef(action);

            Method method = action.getClazz().getInstanceMethod("run", "()Ljava/lang/Object;");
            MethodInvokeLogic.invokeMethod(frame, method);
        }
    };

    public static void init() {
        Registry.register(jlClass, "doPrivileged", "(Ljava/security/PrivilegedAction;)Ljava/lang/Object;", doPrivileged);

        Registry.register(jlClass, "doPrivileged", "(Ljava/security/PrivilegedExceptionAction;)Ljava/lang/Object;", doPrivileged);

        Registry.register(jlClass, "getStackAccessControlContext", "()Ljava/security/AccessControlContext;", new NativeMethod() {
            public void execute(Frame frame) {
                frame.getOperandStack().pushRef(null);
            }
        });
    }
}
